package com.collect.cameroon.zhiAdater.zhiCustomerOrder;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.collect.cameroon.MyApplication;
import com.collect.cameroon.R;
import com.collect.cameroon.zhiAdater.zhiLevelDetail.ZHILevelWalletMobileAdjuster;
import com.collect.cameroon.zhiBean.zhiReceive.ZHIRCustomerRepayOrderListEntity;
import com.collect.cameroon.zhiGlobal.zhiAppSettings.ZHIProjectConfigs;
import com.collect.cameroon.zhiHelper.ZHISysShareHelper;
import com.collect.cameroon.zhiUtils.TextViewUtil;

public class ZHICustomerOrderAdjuster extends BaseQuickAdapter<ZHIRCustomerRepayOrderListEntity, BaseViewHolder> {

    public ZHICustomerOrderAdjuster() {
        super(R.layout.item_customer_repay_order_list, null);
    }

    private OnCTCustomerOrderAdjusterItemClickListener mOnCTCustomerOrderAdjusterItemClickListener;

    @Override
    protected void convert(BaseViewHolder helper, ZHIRCustomerRepayOrderListEntity item) {
        try {
            helper.setText(R.id.tv_orderId, item.getOrderId());
            helper.setText(R.id.tv_name, item.getFullName());
            helper.setText(R.id.tv_mobile, item.getMobileEncryptFormatter());
            helper.setText(R.id.tv_amount, item.getRepayAmountFormat());
            helper.setText(R.id.tv_repay_time, item.getRepayTime());
            helper.setText(R.id.tv_product, item.getProduct());
            helper.setText(R.id.tv_whatsapp_count, item.getFollowNum());
            helper.setText(R.id.tv_tag_num, item.getFollowNum());
            helper.setText(R.id.tv_repay_url, item.getRepayUrl());
            helper.setText(R.id.tv_repay_bank_name, item.getVirtualBankName());
            helper.setText(R.id.tv_repay_bank_num, item.getVirtualBankAccount());
            helper.setText(R.id.tv_remain_repay_amount, item.getRemainRepayAmountFormat());
            helper.setText(R.id.tv_repaid_amount, item.getPartRepayAmountFormat());
            helper.setText(R.id.tv_contact_result, item.getContactResult());
            helper.setText(R.id.tv_icrol_status, item.getContactResult());
            helper.setText(R.id.tv_remark, item.getRemarkSpanFormat());

            ZHILevelWalletMobileAdjuster mZHILevelWalletMobileAdjuster = new ZHILevelWalletMobileAdjuster();

            LinearLayout ll_icrol_follow_bg = helper.getView(R.id.ll_icrol_follow_bg);
            if (item.getFollowNumInt() <= 0) {
                ll_icrol_follow_bg.setVisibility(View.VISIBLE);
            } else {
                ll_icrol_follow_bg.setVisibility(View.GONE);
            }

            //helper.setVisible(R.id.tv_contact_result, !TextUtils.isEmpty(item.getContactResult()));

            LinearLayout ll_settle_logo_bg = helper.getView(R.id.ll_settle_logo_bg);
            if (item.isSettleFlag()) {
                ll_settle_logo_bg.setVisibility(View.VISIBLE);
                ll_icrol_follow_bg.setVisibility(View.GONE);
            } else {
                ll_settle_logo_bg.setVisibility(View.GONE);
            }

            helper.getView(R.id.ll_settle_logo_bg).setOnClickListener(v -> {
                Log.d(TAG, "ll_settle_logo_bg: click");
            });

            if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
                helper.getView(R.id.tv_follow).setOnClickListener(view -> mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_FOLLOW, item));
            }

            helper.getView(R.id.tv_remain_repay_amount).setOnClickListener(v -> mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_REPAID, item));
            helper.getView(R.id.tv_repaid_amount).setOnClickListener(v -> mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_REPAID, item));
            helper.getView(R.id.iv_repaid_amount).setOnClickListener(v -> mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_REPAID, item));

            helper.getView(R.id.tv_mobile).setOnClickListener(v -> ZHISysShareHelper.callPhone(MyApplication.getAppContext(), item.getMobile()));
            helper.getView(R.id.iv_call_phone).setOnClickListener(v -> ZHISysShareHelper.callPhone(MyApplication.getAppContext(), item.getMobile()));
            if (!TextUtils.isEmpty(item.getWhatsAppContent())) {
                helper.getView(R.id.iv_whatsapp).setOnClickListener(v -> {
                    if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
                        mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_WHATSAPP, item);
                    }
                    ZHISysShareHelper.sendMsgWithWhatsApp(mContext, ZHIProjectConfigs.PRE_PHONE + item.getMobile(), item.getWhatsAppContent());
                });
            }

            TextView tv_repay_url = helper.getView(R.id.tv_repay_url);
            if (TextUtils.isEmpty(item.getRepayUrl())) {
                tv_repay_url.setVisibility(View.GONE);
            } else {
                tv_repay_url.setVisibility(View.VISIBLE);
            }

            TextView tv_repay_url_copy = helper.getView(R.id.tv_repay_url_copy);
            TextView tv_repay_url_create = helper.getView(R.id.tv_repay_url_create);
            TextViewUtil.setUnderLine(tv_repay_url_copy);
            TextViewUtil.setUnderLine(tv_repay_url_create);
            tv_repay_url_copy.setOnClickListener(v -> {
                if (TextUtils.isEmpty(item.getRepayUrl())) {
                    return;
                }
                ZHISysShareHelper.copyToClipboard(mContext, item.getRepayUrl());
            });
            tv_repay_url_create.setOnClickListener(v -> {
                if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
                    mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_BANK_ACCOUNT, item);
                }
            });

            RecyclerView rc_wallet_list = helper.getView(R.id.rc_wallet_list);
            rc_wallet_list.setLayoutManager(new GridLayoutManager(mContext, 1));
            rc_wallet_list.setAdapter(mZHILevelWalletMobileAdjuster);

            mZHILevelWalletMobileAdjuster.setOnZHILevelDetailAdjusterItemClickListener(item1 -> ZHISysShareHelper.callPhone(MyApplication.getAppContext(), item1));
            mZHILevelWalletMobileAdjuster.setNewData(item.getMobileList());

//            RelativeLayout rl_bank_account_bg = helper.getView(R.id.rl_bank_account_bg);
//            rl_bank_account_bg.setVisibility(item.isValidVirtualBankAccount() ? View.VISIBLE : View.GONE);
//            rl_bank_account_bg.setOnClickListener(v -> {
//                if (item.isValidVirtualBankAccount()) {
//                    if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
//                        mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_WHATSAPP, item);
//                    }
//                    getOfflineCopyInfo(item.getMobile(), item.getFullName(), item.getRepayAmountFormat(), item.getVirtualBankName(), item.getVirtualBankAccount(), item.getProduct());
//                }
//            });

//            TextView tv_get_bank_account_des = helper.getView(R.id.tv_get_bank_account_des);
//            tv_get_bank_account_des.setOnClickListener(v -> {
//                if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
//                    mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_BANK_ACCOUNT, item);
//                }
//            });

//            ImageView iv_get_bank_account = helper.getView(R.id.iv_get_bank_account);
//            iv_get_bank_account.setOnClickListener(v -> {
//                if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
//                    mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_BANK_ACCOUNT, item);
//                }
//            });


//            ZHINextStepView btn_get_bank_account = helper.getView(R.id.btn_get_bank_account);
//            btn_get_bank_account.setVisibility(item.isValidVirtualBankAccount() ? View.GONE : View.VISIBLE);
//            btn_get_bank_account.setOnMenuItemDidChangedListener(menuItem -> {
//                if (mOnCTCustomerOrderAdjusterItemClickListener != null) {
//                    mOnCTCustomerOrderAdjusterItemClickListener.OnItemClick(CustomerOrderAdjusterItemClickType.TYPE_BANK_ACCOUNT, item);
//                }
//            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setOnCTCustomerOrderAdjusterItemClickListener(OnCTCustomerOrderAdjusterItemClickListener mOnCTCustomerOrderAdjusterItemClickListener) {
        this.mOnCTCustomerOrderAdjusterItemClickListener = mOnCTCustomerOrderAdjusterItemClickListener;
    }

    public interface OnCTCustomerOrderAdjusterItemClickListener {
        void OnItemClick(int type, ZHIRCustomerRepayOrderListEntity zhirRCustomerRepayOrderListEntity);
    }

    public interface CustomerOrderAdjusterItemClickType {
        int TYPE_WHATSAPP = 1;
        int TYPE_REPAID = 2;
        int TYPE_BANK_ACCOUNT = 3;
        int TYPE_FOLLOW = 4;
    }

    private void getOfflineCopyInfo(String phone, String username, String amount, String bankname, String bankcard, String appname) {

//        ##NAME##
//        This is your repayment account
//        bank name:Wema bank
//        bank card:xxxxxxxxxx

        //Le montant de votre remboursement est: xxx, please transfer to this account according to this amount
        String line = username + ",";
        String line0 = "Le montant de votre remboursement est: " + amount + ",\nVeuillez transférer sur ce compte en fonction de ce montant:";
        String line1 = mContext.getString(R.string.bank_name_flag) + " " + bankname;
        String line2 = mContext.getString(R.string.bank_card_flag) + " " + bankcard;
        String line3 = "app: " + appname;
        String content = line + "\n" + line0 + "\n" + line1 + "\n" + line2 + "\n" + line3 + "\n";
        Log.d(TAG, "getOfflineCopyInfo: " + content);

        ZHISysShareHelper.copyToClipboard(mContext, content);

        ZHISysShareHelper.sendMsgWithWhatsApp(mContext, ZHIProjectConfigs.PRE_PHONE + phone, content);

    }
}
